#!/usr/bin/env python

LIWE_PERMS = {}

from user_class import User
import sys
def list ( liwe, gid = 0, name = '', descr = '', uid = -1, _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0, full = '' ):
	"""
	:INPUT:

		- gid:						SAFE_NUMBER
		- name:						SAFE_DB_STRING
		- descr:					SAFE_DB_STRING
		- uid:						SAFE_NUMBER
		- _X_LINES:					SAFE_NUMBER
		- _X_START_POINT:				SAFE_NUMBER
		- _X_PAGE:					SAFE_NUMBER
		- full: if set it will return perms too 	SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	limit = _X_LINES

	uc = User ( liwe )
	res = uc.list_groups ( gid = gid, name = name, descr = descr, uid = uid, start_point = start_point, limit = limit, full = full )

	count = 0
	for v in res [ 'rows' ]:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v

		v [ '_img' ] = ''

		count += 1

	res [ 'rows' ] = res [ 'tot_rows' ]

	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = _X_LINES

	return res

def create ( liwe, gid = 0, name = '', descr = '' ): 
	"""
	:INPUT:

		- gid:			SAFE_NUMBER
		- name:			SAFE_DB_STRING
		- descr:		SAFE_DB_STRING

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )

	res = uc.create_group ( gid = gid, name = name, descr = descr )

	return { "gid" : res }

def delete ( liwe, ids ):
	"""
	:INPUT:

		- ids:		SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )
	uc.delete_group ( ids )

def get ( liwe, gid = 0, users = '' ):
	"""
	:INPUT:

		- gid:								SAFE_NUMBER
		- users:	if set it will return group related users 	SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	return { 'group': uc.get_group ( gid, users ) }

def get_user_group ( liwe, uid = 0 ):
	"""
	:INPUT:
		-uid:	SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	
	return  uc.get_user_group ( uid ) 

def get_group_user ( liwe, gid = 0, not_in_group = '', uid = 0, login = '', email = '' ): 
	"""
	:INPUT:

		- gid:								SAFE_NUMBER
		- uid:								SAFE_NUMBER
		- login:							SAFE_DB_STRING
		- email:							SAFE_DB_STRING
		- not_in_group:	if set it will return users not in group 	SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	return { 'users' : uc.get_group_user ( gid = gid, not_in_group = not_in_group, uid = uid, login = login, email = email ) }

def get_group_range ( liwe, gid = '', uid = 0, login = '', email = '', flat = 0 ):
	"""
	:INPUT:

		- gid:								SAFE_DB_STRING
		- uid:								SAFE_NUMBER
		- login:							SAFE_DB_STRING
		- email:							SAFE_DB_STRING
		- flat:								SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	res = uc.get_group_range ( gid = gid, uid = uid, login = login, email = email, flat = flat )

	return { 'user' : res }

def save_user_group ( liwe, uid = 0, gids = '' ):
	"""
	:INPUT:

		- uid:	SAFE_NUMBER
		- gids: SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	uc.save_user_group ( uid = uid, gids = gids )

def save_group_user ( liwe, gid = 0, ids = '' ):
	"""
	:INPUT:

		- gid:						SAFE_NUMBER
		- ids: list of users' ids separated by pipe	SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	uc.save_group_user ( gid = gid, ids = ids )
def empty_group_user ( liwe, gid = 0, mode ="group" ):
	"""
	:INPUT:

		- gid:		SAFE_NUMBER
		- mode:		SAFE_STRING			

	:PERMS:
		- create:		With this permission you can create/modify groups
	"""
	uc = User ( liwe )
	uc.empty_group_user ( gid = gid, mode = mode )
